Skip to content

Conversation

@karlseguin
Copy link
Collaborator

…erval

When the browser microtask was added, zig-specific timeout functions were added to the loop. This was necessary for two reasons: 1 - The existing functions were JS specific
2 - We wanted a different reset counter for JS and Zig

Like we did in #577, the loop is now JS-agnostic. It gets a Zig callback, and the Zig callback can execute JS (or do whatever). An intrusive node, like with events, is used to minimize allocations.

Also, because the microtask was recently moved to the page, there is no longer a need for separate event counters. All timeouts are scoped to the page.

The new timeout callback can now be used to efficiently reschedule a task. This reuses the IO.completion and Context, avoiding 2 allocations. More importantly it makes the internal timer_id static for the lifetime of an "interval". This is important for window.setInterval, where the callback can itself clear the interval, which we would need to detect in the callback handler to avoid re-scheduling. With the stable timer_id, the existing cancel mechanism works as expected.

The loop no longer has a cbk_error. Callback code is expected to try/catch callbacks (or use callback.tryCall) and handle errors accordingly.

@karlseguin karlseguin force-pushed the timeouts_and_intervals branch from 86894f2 to f04b326 Compare May 5, 2025 11:03
…erval

When the browser microtask was added, zig-specific timeout functions were
added to the loop. This was necessary for two reasons:
1 - The existing functions were JS specific
2 - We wanted a different reset counter for JS and Zig

Like we did in #577, the loop is
now JS-agnostic. It gets a Zig callback, and the Zig callback can execute JS
(or do whatever). An intrusive node, like with events, is used to minimize
allocations.

Also, because the microtask was recently moved to the page, there is no longer
a need for separate event counters. All timeouts are scoped to the page.

The new timeout callback can now be used to efficiently reschedule a task. This
reuses the IO.completion and Context, avoiding 2 allocations. More importantly
it makes the internal timer_id static for the lifetime of an "interval". This
is important for window.setInterval, where the callback can itself clear the
interval, which we would need to detect in the callback handler to avoid
re-scheduling. With the stable timer_id, the existing cancel mechanism works
as expected.

The loop no longer has a cbk_error. Callback code is expected to try/catch
callbacks (or use callback.tryCall) and handle errors accordingly.
@karlseguin karlseguin force-pushed the timeouts_and_intervals branch from f04b326 to 01aa826 Compare May 5, 2025 11:03
@krichprollsch krichprollsch merged commit f1fe4c0 into main May 6, 2025
12 checks passed
@krichprollsch krichprollsch deleted the timeouts_and_intervals branch May 6, 2025 13:18
@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants